﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Rehabilitation.Models;

namespace Rehabilitation.Controllers
{
    public class LocationController : Controller
    {
        private DBEntities db = new DBEntities();


        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);

            if (!Request.IsAuthenticated || Session["User"] == null)
            {
                Response.Redirect("/Account/LogOff");
            }
        }


        //
        // GET: /Location/

        public ActionResult Index()
        {
            return View(db.Locations.ToList());
        }

        //
        // GET: /Location/Details/5

        public ActionResult Details(long id = 0)
        {
            Location location = db.Locations.Single(l => l.LocationID == id);
            if (location == null)
            {
                return HttpNotFound();
            }
            return View(location);
        }

        //
        // GET: /Location/Create

        public ActionResult Create()
        {
            List<Location> locations = db.Locations.ToList();
            if (locations.Any())
            {
                List<SelectListItem> locationList = new List<SelectListItem>();
                foreach (Location l in locations)
                {
                    locationList.Add(new SelectListItem() { Text = l.Name, Value = l.LocationID.ToString() });
                }
                ViewBag.LocationList = locationList;
            }
            return View();
        }

        //
        // POST: /Location/Create

        [HttpPost]
        public ActionResult Create(Location location)
        {
            if (ModelState.IsValid)
            {
                db.Locations.AddObject(location);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(location);
        }

        //
        // GET: /Location/Edit/5

        public ActionResult Edit(long id = 0)
        {
            Location location = db.Locations.Single(l => l.LocationID == id);
            if (location == null)
            {
                return HttpNotFound();
            }
            List<Location> locations = db.Locations.ToList();
            if (locations.Any())
            {
                List<SelectListItem> locationList = new List<SelectListItem>();
                foreach (Location l in locations)
                {
                    locationList.Add(new SelectListItem() { Text = l.Name, Value = l.LocationID.ToString(), Selected = l.LocationID == location.ParentID});
                }
                ViewBag.LocationList = locationList;
            }
            ViewBag.Location = location;
            return View(location);
        }

        //
        // POST: /Location/Edit/5

        [HttpPost]
        public ActionResult Edit(Location location)
        {
            if (ModelState.IsValid)
            {
                db.Locations.Attach(location);
                db.ObjectStateManager.ChangeObjectState(location, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(location);
        }

        //
        // GET: /Location/Delete/5

        public ActionResult Delete(long id = 0)
        {
            Location location = db.Locations.Single(l => l.LocationID == id);
            if (location == null)
            {
                return HttpNotFound();
            }
            return View(location);
        }

        //
        // POST: /Location/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {
            Location location = db.Locations.Single(l => l.LocationID == id);
            db.Locations.DeleteObject(location);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}